package com.example.jdk21;

import static java.lang.StringTemplate.STR;
import static java.util.FormatProcessor.FMT;

public class TemplateExpressions {
    public static void main(String[] args) {
        String blog = "blog.didispace.com";
        String str = STR."My blog is \{blog}";
        System.out.println(str);

        String html = """
                    <html>
                    <body>
                      <h1>Java 15 新特性：文本块 ｜ 程序猿DD</h1>
                      <p>didispace.com</p>
                    </body>
                    </html>
                    """;
        System.out.println(html);
        String name = "dejun.yang";
        int age = 18;
        var json = STR."""
                    {
                        "user": "\{name}",
                        "age: \{age}
                    }
                    """;
        System.out.println(json);

        int x = 10, y = 20;
        String s = STR."\{x} + \{y} = \{x + y}";
        System.out.println(s);

        String s2 = STR."My blog is \{getMyBlog()}";
        System.out.println(s2);

        User u = new User("didi", "blog.didispace.com"); // 构造函数参数为name属性和blog属性
        String s3 = STR."\{u.name}的博客地地址为：+ \{u.blog}";
        System.out.println(s3);

        record Rectangle(String name, double width, double height) {
            double area() {
                return width * height;
            }
        }
        Rectangle[] zone = new Rectangle[] {
                new Rectangle("Alfa", 17.8, 31.4),
                new Rectangle("Bravo", 9.6, 12.4),
                new Rectangle("Charlie", 7.1, 11.23),
        };

        String table = FMT."""
            Description     Width    Height     Area
            %-12s\{zone[0].name}  %7.2f\{zone[0].width}  %7.2f\{zone[0].height}     %7.2f\{zone[0].area()}
            %-12s\{zone[1].name}  %7.2f\{zone[1].width}  %7.2f\{zone[1].height}     %7.2f\{zone[1].area()}
            %-12s\{zone[2].name}  %7.2f\{zone[2].width}  %7.2f\{zone[2].height}     %7.2f\{zone[2].area()}
            \{" ".repeat(28)} Total %7.2f\{zone[0].area() + zone[1].area() + zone[2].area()}
            """;

        System.out.println(table);


    }


    private static String getMyBlog() {
        return "hello";
    }

}

class User {
    String name;
    String blog;
    public User(String name, String blog) {
        this.name = name;
        this.blog = blog;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", blog='" + blog + '\'' +
                '}';
    }
}
